package net.ecpt.hrm.services;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.ExcelPoiUtils;

import javolution.util.FastList;
import javolution.util.FastMap;

public class HrCandidateInfoXlsServices {
    public static final String module = HrCandidateInfoXlsServices.class.getName();


    /**
     * 导入
     */
    public static Map<String, Object> importXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
	Delegator delegator = dctx.getDelegator();
	List<GenericValue> toBeStore = new ArrayList();
	List<String> messageList = new ArrayList<String>();
	List<Map<String, Object>> sheetList = (List<Map<String, Object>>) context.get("sheetList");
	LocalDispatcher dispatcher = dctx.getDispatcher();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	String createBy = userLogin.getString("partyId");
	
	try {
	    int i = 0;
	   
	    for (Map<String, Object> row : sheetList) {
		i = i + 1;
		
			String id = ExcelPoiUtils.getCellString(row, 0);
			String name = ExcelPoiUtils.getCellString(row, 1);
			String gender_dictText = ExcelPoiUtils.getCellString(row, 2);
			String age = ExcelPoiUtils.getCellString(row, 3);
			String wedlock_dictText = ExcelPoiUtils.getCellString(row, 4);
			String email = ExcelPoiUtils.getCellString(row, 5);
			String phone = ExcelPoiUtils.getCellString(row, 6);
			String specialty = ExcelPoiUtils.getCellString(row, 7);
			String school = ExcelPoiUtils.getCellString(row, 8);
			String education_dictText = ExcelPoiUtils.getCellString(row, 9);
			String position = ExcelPoiUtils.getCellString(row, 10);
			String expectedSalary = ExcelPoiUtils.getCellString(row, 11);
			String writeDate = ExcelPoiUtils.getCellString(row, 12);
			String startDate_dictText = ExcelPoiUtils.getCellString(row, 13);
			//编号 姓名  性别   年龄   婚姻状况  邮箱  手机  所学专业 毕业院校  学历  应聘职位  期望工资  填表日期  上岗时间
			
			// 判断
			if (UtilValidate.isEmpty(name)) {
			    messageList.add("第" + (i + 1) + "行,姓名不能为空。");
			    continue;
			}

			// 判断
			if (UtilValidate.isEmpty(gender_dictText)) {
			    messageList.add("第" + (i + 1) + "行,性别不能为空。");
			    continue;
			}
			// 判断
			if (UtilValidate.isEmpty(age)) {
			    messageList.add("第" + (i + 1) + "行,年龄不能为空。");
			    continue;
			}
			// 判断
			if (UtilValidate.isEmpty(wedlock_dictText)) {
			    messageList.add("第" + (i + 1) + "行,婚姻状况能为空。");
			    continue;
			}
			// 判断
			if (UtilValidate.isEmpty(email)) {
			    messageList.add("第" + (i + 1) + "行,邮箱不能为空。");
			    continue;
			}

			if (UtilValidate.isEmpty(phone)) {
			    messageList.add("第" + (i + 1) + "行,手机不能为空。");
			    continue;
			}
			// 判断
			if (UtilValidate.isEmpty(specialty)) {
			    messageList.add("第" + (i + 1) + "行,所学专业不能为空。");
			    continue;
			}
			// 判断
			if (UtilValidate.isEmpty(school)) {
			    messageList.add("第" + (i + 1) + "行,毕业院校不能为空。");
			    continue;
			}
			// 判断
			if (UtilValidate.isEmpty(education_dictText)) {
			    messageList.add("第" + (i + 1) + "行,学历不能为空。");
			    continue;
			}
			
			// 判断
			if (UtilValidate.isEmpty(position)) {
			    messageList.add("第" + (i + 1) + "行,应聘职位不能为空。");
			    continue;
			}
			
			// 判断
			if (UtilValidate.isEmpty(expectedSalary)) {
			    messageList.add("第" + (i + 1) + "行,期望工资不能为空。");
			    continue;
			}
			
			// 判断
			if (UtilValidate.isEmpty(writeDate)) {
			    messageList.add("第" + (i + 1) + "行,填表日期不能为空。");
			    continue;
			}
			
			// 判断
			if (UtilValidate.isEmpty(startDate_dictText)) {
			    messageList.add("第" + (i + 1) + "行,上岗时间不能为空。");
			    continue;
			}
			
			// 创建
			GenericValue entity = null;
			if (UtilValidate.isNotEmpty(id)) {
			   
			    List<GenericValue> entityList = delegator.findByAnd("HrCandidateInfo",
					    UtilMisc.toMap("id", id), null, false);
				    entity = EntityUtil.getFirst(entityList);
				if (UtilValidate.isEmpty(entity)) {
						 id = delegator.getNextSeqId("HrCandidateInfo");
						 entity = delegator.makeValue("HrCandidateInfo", UtilMisc.toMap("id", id));
						 entity.set("statusId", "0");
							entity.set("createBy", createBy);
							entity.set("createDate", nowTimestamp);
				}
					   
			}else  {
			    id = delegator.getNextSeqId("HrCandidateInfo");
			    entity = delegator.makeValue("HrCandidateInfo", UtilMisc.toMap("id", id));
			    entity.set("statusId", "0");
				entity.set("createBy", createBy);
				entity.set("createDate", nowTimestamp);
			} 
			
			
			
			
			String gender = ExcelPoiUtils.getDataDictionaryCode(delegator, "USER_SEX",gender_dictText);
			String education = ExcelPoiUtils.getDataDictionaryCode(delegator, "EMP_EDUCATION",education_dictText);
			//String position = ExcelPoiUtils.getDataDictionaryCode(delegator, "POSITION",position_dictText);
			String startDate = ExcelPoiUtils.getDataDictionaryCode(delegator, "START_DATE",startDate_dictText);
			String wedlock = ExcelPoiUtils.getDataDictionaryCode(delegator, "EMP_MARITAL_STATUS",wedlock_dictText);
			
			
			Timestamp writeDateDateTimestamp =UtilDateTime.toTimestamp(writeDate);
			//Timestamp startDateTimestamp =UtilDateTime.toTimestamp(startDate);
			
			entity.set("name", name);
			entity.set("gender", gender);
			entity.set("age", age);
			entity.set("wedlock", wedlock);
			entity.set("email", email);
			entity.set("phone", phone);
			entity.set("specialty", specialty);
			entity.set("school", school);
			entity.set("education", education);
			entity.set("position", position);
			entity.set("expectedSalary", expectedSalary);
			entity.set("writeDate", writeDateDateTimestamp);
			entity.set("startDate", startDate);
			
			toBeStore.add(entity);
			
			
		
	    }
	    delegator.storeAll(toBeStore);
	    result.put("messageList", messageList);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    /**
     * 导出
     */

    public static Map<String, Object> exportXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Delegator delegator = dctx.getDelegator();
	String ids = (String) context.get("ids");
	List<String> baseIdList = FastList.newInstance();
	if (UtilValidate.isNotEmpty(ids)) {
	    baseIdList = Arrays.asList(ids.split(";"));
	}
	Map<String, Object> result = ServiceUtil.returnSuccess();

	EntityCondition mainCond = null;
	List<EntityCondition> andExprs = FastList.newInstance();
	//andExprs.add(EntityCondition.makeCondition("parentProductId", EntityOperator.EQUALS, "_NA_"));
	if (UtilValidate.isNotEmpty(baseIdList))
	    andExprs.add(EntityCondition.makeCondition("id", EntityOperator.IN, baseIdList));
	if (andExprs.size() > 0)
	    mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);

	try {
	    List<GenericValue> entityList = delegator.findList("HrCandidateInfo", mainCond, null, null, null, true);

	    List<Map<String, Object>> list = FastList.newInstance();
	    Map<String, Object> keyMap = FastMap.newInstance();
	    keyMap.put("0", "id");
	    keyMap.put("1", "name");
	    keyMap.put("2", "gender_dictText");
	    keyMap.put("3", "age");
	    keyMap.put("4", "wedlock");
	    keyMap.put("5", "email");
	    keyMap.put("6", "phone");
	    keyMap.put("7", "specialty");
	    keyMap.put("8", "school");
	    keyMap.put("9", "education_dictText");
	    keyMap.put("10", "position");
	    keyMap.put("11", "expectedSalary");
	    keyMap.put("12", "writeDate");
	    keyMap.put("13", "startDate");
	    String headers = "编号,姓名 ,性别 ,年龄,婚姻状况 ,邮箱,手机,所学专业,毕业院校,学历,应聘职位,期望工资,填表日期,上岗时间";
	    result.put("headers", headers);
	    
	    

	    for (GenericValue entity : entityList) {
		Map<String, Object> map = FastMap.newInstance();
		String id = entity.getString("id");
		String name = entity.getString("name");
		String gender = entity.getString("gender");
		String age = entity.getString("age");
		String wedlock = entity.getString("wedlock");
		String email = entity.getString("email");
		String phone = entity.getString("phone");
		String specialty = entity.getString("specialty");
		String school = entity.getString("school");
		String education = entity.getString("education");
		String position = entity.getString("position");
		String expectedSalary = entity.getString("expectedSalary");
		Timestamp writeDate = entity.getTimestamp("writeDate");
		String startDate = entity.getString("startDate");
		
		String gender_dictText = ExcelPoiUtils.getDataDictionaryDescription(delegator, "USER_SEX",gender);
		String wedlock_dictText = ExcelPoiUtils.getDataDictionaryDescription(delegator, "EMP_MARITAL_STATUS",wedlock);
	    String education_dictText = ExcelPoiUtils.getDataDictionaryDescription(delegator, "EMP_EDUCATION",education);
	    String startDate_dictText = ExcelPoiUtils.getDataDictionaryDescription(delegator, "START_DATE",startDate);
	    
	    
	    
	    map.put("id", id);
		map.put("name", name);
		map.put("gender_dictText", gender_dictText);
		map.put("age", age);
		map.put("wedlock", wedlock_dictText);
		map.put("email", email);
		map.put("phone", phone);
		map.put("specialty", specialty);
		map.put("school", school);
		map.put("education_dictText", education_dictText);
		map.put("position", position);
		map.put("expectedSalary", expectedSalary);
		map.put("writeDate", writeDate);
		map.put("startDate", startDate_dictText);

		list.add(map);
	    }
	    result.put("list", list);
	    result.put("keyMap", keyMap);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    
}
